package com.audaque.springboot.foshanupload.webflux.filter;

import com.audaque.springboot.foshanupload.webflux.holder.ReactiveHttpContextHolder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @author zgb
 * @desc ...
 * @date 2023-08-07 17:52:51
 */
@Component
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public class ServerWebExchange2ContextHolderFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        //设置当前请求
        return chain.filter(exchange)
                .subscriberContext(context -> context.put(ReactiveHttpContextHolder.Info.SERVERWEBEXCHANGE_KEY, exchange));
    }
}
